import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {

    public static void main(String[] args) throws InterruptedException {
     //  test01();
      //test02();
       //这个test03会报错,因为的是非 form标签中的 新闻超链接
       //test03();
        //test04();
       // test05();
        //test06();
        //test07();
    }

    private static void test07() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //隐式等待代码

        //判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver,3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(8)")));


        
    }

    private static void test06() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") &&title.equals("百度一下，你就知道") ){
            System.out.println("当前页面url="+url+"当前页面title"+title);
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");

        }

    }

    private static void test05() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");

        //找到百度搜索输入框,输入"Java107"
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Java107");
        //点击了百度一下 按钮
        //webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#su")).submit();
       webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);

        //清空百度搜索输入框的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();


    }

    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
       // System.out.println(button_value);
        if(button_value.equals("百度一下")){
            System.out.println("测试通过");

        }else{
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();

    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        //找到百度搜索输入框,输入"Java107"
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Java107");
        //点击了百度一下 按钮
        //webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#su")).submit();
        sleep(3000);

        //清空百度搜索输入框的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();


    }

    private static void test01() throws InterruptedException {
        int flag = 0;

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");

        //找到百度搜索输入框
        // WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));

        //输入软件测试
        element.sendKeys("软件测试");

        //找到百度一下按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);


        //校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for(int i = 0;i<elements.size();i++){
           // System.out.println(elements.get(i).getText());
            //如果返回的结果有软件测试,证明测试通过,否则测试不通过
            if(elements.get(i).getText().contains("测试")||elements.get(i).getText().contains("测试")){
                flag = 1;
                System.out.println("测试通过");
                break;
            }

        }
        if(flag == 0){
            System.out.println("测试不通过");
        }

    }
}